import {
    PlayGroundWidth,
    PlayGroundHeight,
} from "./canvas.js";
import { Tank } from "./tank.js";
import { TikTok, tankStore } from "./utils.js";

export class Robot {
    constructor(opt = {}) {
        this.tank = new Tank({
            parent: this,
            name: opt.name,
            type: opt.type,
            defaultDirection: "down",
            coord: {
                x: opt.coord.x,
                y: opt.coord.y,
            },
            beShotCb: byBullet => {
                if (byBullet.parent.parent && byBullet.parent.parent.getType() === "robot") {
                    // robot不能击毁robot
                } else {
                    if (this.tank.attr.blood === 2 && byBullet.power === 1) {
                        this.tank.switchType("normalRobot");
                    } else {
                        // 被击毁
                        this.destroy();
                    }
                }
            },
            canNotMove: () => {
                this._changeDirection();
            },
        });
        tankStore.push(this.tank);
        this._direction = "";
        this._changeDirection = () => {
            if (!this.tank) {
                return;
            }
            let _chance = {
                keep: 0.5,
                up: 0.125,
                left: 0.125,
                right: 0.125,
                down: 0.125,
            };
            if (this.tank.coord.x < PlayGroundWidth / 2) {
                if (this.tank.coord.y < PlayGroundHeight / 2) {
                    Object.assign(_chance, {
                        up: 0.075,
                        left: 0.075,
                        right: 0.175,
                        down: 0.175,
                    });
                } else {
                    Object.assign(_chance, {
                        up: 0.175,
                        left: 0.075,
                        right: 0.175,
                        down: 0.075,
                    });
                }
            } else {
                if (this.tank.coord.y < PlayGroundHeight / 2) {
                    Object.assign(_chance, {
                        up: 0.075,
                        left: 0.175,
                        right: 0.075,
                        down: 0.175,
                    });
                } else {
                    Object.assign(_chance, {
                        up: 0.175,
                        left: 0.175,
                        right: 0.075,
                        down: 0.075,
                    });
                }
            }

            let roll = Math.random();
            if (roll < _chance.keep) {
                // keep direction
            } else if (roll < _chance.keep + _chance.up) {
                this.go("up");
            } else if (roll < _chance.keep + _chance.up + _chance.left) {
                this.go("left");
            } else if (roll < _chance.keep + _chance.up + _chance.left + _chance.right) {
                this.go("right");
            } else {
                this.go("down");
            }
        };

        setTimeout(() => {
            if (this.tank) {
                this.tank.fire(true);       // robot 持续 fire
                this.go("down");
            }
            TikTok.register(this._changeDirection, 400);
        }, 600);
    }

    getType() {
        return "robot";
    }

    go(direction) {
        if (!this.tank) {
            return;
        }
        if (this._direction.length) {
            this.tank.onEvent("keyup", this._direction);
        }
        this.tank.onEvent("keydown", direction);
        this._direction = direction;
    }

    stop() {
        if (this.tank) {
            this.tank.onEvent("keyup", this._direction);
        }
    }

    destroy() {
        this.stop();
        if (this.tank) {
            this.tank.fire(false);
            this.tank.destroy();
            this.tank = null;
        }
        TikTok.unRegister(this._changeDirection);
        let _index = RobotController._robots.findIndex(_robot => _robot === this);
        RobotController._robots.splice(_index, 1);
        if (RobotController._index >= RobotController.totalRobots && !RobotController._robots.length) {
            RobotController.destroy();
            alert("Congurations! You Win!");
        }
    }
}

const robotType = ["beeRobot", "normalRobot", "strongerRobot"];
const robotCoord = [
    { x: 1, y: 1 },
    { x: 106, y: 0 },
    { x: 210, y: 1 },
];
function findCoord() {
    let res = robotCoord[Math.floor(Math.random() * 9 / 3)];
    for (let i = 0; i < RobotController._robots.length; i++) {
        let _robot = RobotController._robots[i];
        if (_robot.tank && _robot.tank.coord) {
            if (_robot.tank.coord.x >= res.x && _robot.tank.coord.x <= res.x + 10 && _robot.tank.coord.y >= res.y && _robot.tank.coord.y <= res.y + 10) {
                return findCoord();
            }
        }
    }
    return res;
}

export const RobotController = {
    maxRobots: 6,
    totalRobots: 20,
    _robots: [],
    _index: 0,
    init() {
        this.born({
            name: "robot1",
            type: robotType[0],
            coord: {
                ...robotCoord[0],
            },
        });
        this.born({
            name: "robot2",
            type: robotType[1],
            coord: {
                ...robotCoord[1],
            },
        });
        this.born({
            name: "robot3",
            type: robotType[2],
            coord: {
                ...robotCoord[2],
            },
        });
        this._index = 3;
        this._bornRobot = () => {
            if (this._robots.length < this.maxRobots && this._index < this.totalRobots) {
                this.born({
                    name: `robot${this._index + 1}`,
                    type: robotType[Math.floor(Math.random() * 9 / 3)],
                    coord: {
                        ...findCoord(),
                    },
                });
                this._index++;
            }
        };
        TikTok.register(this._bornRobot, 800);
    },
    born(...opt) {
        let _robot = new Robot(...opt);
        this._robots.push(_robot);
    },
    destroy() {
        TikTok.unRegister(this._bornRobot);
        this._robots.length = 0;
    },
};
